﻿var sDept;
var smode = "";
var prog_name = "AnnouncementDetail.js";
function getDept(sDEPT_NO) {
    try
    {
    $("input[id*='btnAdd']").attr("disabled", true);
    var s_PARENT_DEPT_ID = "";
    if (smode == "add" || smode == "edit")
    {
        $.ajax({
            type: "POST",
            url: "AnnouncementDetail.aspx/getDept",
            data: "{'DEPT_NO' : '" + sDEPT_NO + "'}",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            error: function (xmlHttpRequest, error) {
                var m_ErrMessage = xmlHttpRequest.responseText;
                alert(m_ErrMessage);
            },
            success: function (data) {
                if (data != null) {
                    if (data.d.indexOf("error") >= 0) {
                        if (data.d.indexOf("error_no") < 0) {
                            alert(data.d.replace("error", ""));
                        }
                    }
                    else {
                        sDept = data.d;
                        $("#MainContent_hidDEPTS").val(data.d);
                        //初始總公司下拉
                        getDept_Down("ddlcompany", "0000", "Y");
                        //hidden 初使
                        $("#MainContent_hidcompany").val("ALL");
                        $("#MainContent_hidbu").val("ALL");
                        $("#MainContent_hidzone").val("ALL");
                        $("#MainContent_hidAREA").val("ALL");
                        $("#MainContent_hidDept").val("ALL");
                        
                        var sVALUE = "";
                        sVALUE = $("#MainContent_hidcompany").val();
                        if (sVALUE != "") {
                            $('#ddlcompany option[value=' + sVALUE + ']').attr('selected', true);
                        }

                        if (sVALUE != "") {
                            //若總公司有選值，則必需展出ddlbu的下拉，將總公司下拉的id 及值代入
                            getDept_Down("ddlcompany", sVALUE, "Y");
                            sVALUE = $("#MainContent_hidbu").val();
                            $('#ddlbu option[value=' + sVALUE + ']').attr('selected', true);
                        }
                        if (sVALUE != "") {
                            getDept_Down("ddlbu", sVALUE, "Y");
                            sVALUE = $("#MainContent_hidzone").val();
                            $('#ddlzone option[value=' + sVALUE + ']').attr('selected', true);
                        }
                        if (sVALUE != "") {
                            getDept_Down("ddlzone", sVALUE, "Y");
                            sVALUE = $("#MainContent_hidAREA").val();
                            $('#ddlAREA option[value=' + sVALUE + ']').attr('selected', true);
                        }
                        if (sVALUE != "") {
                            getDept_Down("ddlAREA", sVALUE, "Y");
                            sVALUE = $("#MainContent_hidDept").val();
                            $('#ddlDept option[value=' + sVALUE + ']').attr('selected', true);
                        }
                        $("input[id*='btnAdd']").attr("disabled", false);

                    }
                }
                
            }
        });
    }
       
    }
    catch (err) {
        txt = "程式發生錯誤！\n";
        txt += "錯誤訊息: " + err.message + "\n\n";
        txt += "請按確定繼續.\n\n";
        alert(txt);
        save_error(prog_name, "getDept", "USER", "js", err);
        return false;
    }
        
}

function getCondition(mode) {
    try
    {
    smode = mode;
    var BULLETIN_ID = $("#MainContent_txt_BULLETIN_ID").val();


    //若為servre端錯誤返回頁面，需把hidden欄位資料放入Grid中
    if ($("#MainContent_hidseq_C").val() != "") {
        var str = $("#MainContent_hidcompany_C").val();
        var ary_company = str.split(",");
        str = $("#MainContent_hidbu_C").val();
        var ary_bu = str.split(",");
        str = $("#MainContent_hidzone_C").val();
        var ary_zone = str.split(",");
        str = $("#MainContent_hidarea_C").val();
        var ary_area = str.split(",");
        str = $("#MainContent_hiddept_C").val();
        var ary_dept = str.split(",");

        str = $("#MainContent_hidcompany_C_T").val();
        var ary_company_t = str.split(",");
        str = $("#MainContent_hidbu_C_T").val();
        var ary_bu_t = str.split(",");
        str = $("#MainContent_hidzone_C_T").val();
        var ary_zone_t = str.split(",");
        str = $("#MainContent_hidarea_C_T").val();
        var ary_area_t = str.split(",");
        str = $("#MainContent_hiddept_C_T").val();
        var ary_dept_t = str.split(",");

        for (var j = 0; j < ary_company.length - 1; j++) {
            Add_Condition(ary_company[j], ary_bu[j], ary_zone[j], ary_area[j], ary_dept[j], ary_company_t[j], ary_bu_t[j], ary_zone_t[j], ary_area_t[j], ary_dept_t[j]);
            
        }
    }
    else {
         //第一次進入頁面
        if (mode != "add") {
            $.ajax({
                type: "POST",
                url: "AnnouncementDetail.aspx/getCondition",
                data: "{'BULLETIN_ID' : '" + BULLETIN_ID + "'}",
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                error: function (xmlHttpRequest, error) {
                    var m_ErrMessage = xmlHttpRequest.responseText;
                    alert(m_ErrMessage);
                },
                success: function (data) {
                    if (data != null) {
                        if (data.d.indexOf("error") >= 0) {
                            alert(data.d.replace("error", ""));
                        }
                        else {
                            $.each(data.d, function (i, item) {
                                //
                                Add_Condition(item.NAME1, item.NAME2, item.NAME3, item.NAME4, item.NAME5, item.DEPT_NAME1, item.DEPT_NAME2, item.DEPT_NAME3, item.DEPT_NAME4, item.DEPT_NAME5)

                            });
                            saveCondition();
                        }
                    }

                }
            });
        }
    }
    }
    catch (err) {
        txt = "程式發生錯誤！\n";
        txt += "錯誤訊息: " + err.message + "\n\n";
        txt += "請按確定繼續.\n\n";
        alert(txt);
        save_error(prog_name, "getCondition", "USER", "js", err);
        return false;
    }
}

function btn_Add_Condition() {
    try
    {
    var company = $("#MainContent_hidcompany").val();
    var bu = $("#MainContent_hidbu").val();
    var zone = $("#MainContent_hidzone").val();
    var AREA = $("#MainContent_hidAREA").val();
    var Dept = $("#MainContent_hidDept").val();

    var t_company = $("#ddlcompany option:selected").text();
    var t_bu = $("#ddlbu option:selected").text();
    var t_zone = $("#ddlzone option:selected").text();
    var t_AREA = $("#ddlAREA option:selected").text();
    var t_Dept = $("#ddlDept option:selected").text();

    var BULLETIN_ID = $("#MainContent_txt_BULLETIN_ID").val();

    Add_Condition(company, bu, zone, AREA, Dept, t_company, t_bu, t_zone, t_AREA, t_Dept);
    //更新Condition至Hidden
    saveCondition();
    
    return false;
    }
    catch (err) {
        txt = "程式發生錯誤！\n";
        txt += "錯誤訊息: " + err.message + "\n\n";
        txt += "請按確定繼續.\n\n";
        alert(txt);
        save_error(prog_name, "btn_Add_Condition", "USER", "js", err);
        return false;
    }

}
function DeleteRow(e, p_ButtonID) {
    try
    {
    var obj = GetEvent(e)
    if ($("#" + obj.id).parent().length > 0) {
        var m_Tr = $("#" + obj.id).parent().parent()[0];
        var index = m_Tr.rowIndex;

        
        var tbody = $("#tabCondition")[0];
        tbody.deleteRow(index);
    }
    var i = 1;
    $("span[id*='lblSEQ']").each(function () {

        this.innerHTML = i;
        i = i + 1;
    });
    i = 1;
    $("input[id*='hidSEQ_D']").each(function () {

        this.value = i;
        i = i + 1;
    });

    //回Server更新資料庫
    //更新Condition至Hidden
    saveCondition();
    }
    catch (err) {
        txt = "程式發生錯誤！\n";
        txt += "錯誤訊息: " + err.message + "\n\n";
        txt += "請按確定繼續.\n\n";
        alert(txt);
        save_error(prog_name, "DeleteRow", "USER", "js", err);
        return false;
    }
}
function Add_Condition(company, bu, zone, AREA, Dept, t_company, t_bu, t_zone, t_AREA, t_Dept) {
    try
    {
    //var tbody = document.getElementById(tblGV);
    var row;
    var rowcut;
    var cell;
    var sHTML
    var tbody = $("#tabCondition")[0];
    //tbody.className = "GridTableHeader";
    tbody.style.cssText = "border:1px solid #c6c6c6;box-shadow: 10px 10px 5px #888888";

    if (tbody.rows.length == 0) {
        //Header
        row = tbody.insertRow(-1);
        for (var j = 0; j < 7; j++) {
            cell = document.createElement("TH");
            cell.className = "DetlCol";
            switch (j) {
                case 0:
                    cell.style.cssText = "width:10%"
                    cell.insertAdjacentHTML("afterBegin", "&nbsp;");
                    break;
                case 1:
                    cell.style.cssText = "width:15%"
                    cell.insertAdjacentHTML("afterBegin", "條件代號");
                    break;
                case 2:
                    cell.style.cssText = "width:15%"
                    cell.insertAdjacentHTML("afterBegin", "公司");
                    break;
                case 3:
                    cell.style.cssText = "width:15%"
                    cell.insertAdjacentHTML("afterBegin", "營總");
                    break;
                case 4:
                    cell.style.cssText = "width:15%"
                    cell.insertAdjacentHTML("afterBegin", "處級");
                    break;
                case 5:
                    cell.style.cssText = "width:15%"
                    cell.insertAdjacentHTML("afterBegin", "區級");
                    break;
                case 6:
                    cell.style.cssText = "width:15%"
                    cell.insertAdjacentHTML("afterBegin", "所級");
                    break;
            }
            row.appendChild(cell);
        }

    }
    //Detail
    if (smode == "delete" || smode == "view") {
        $("input[id*='btnAdd']").attr("disabled", true);
        var sType = "disabled=true"
    }
    row = tbody.insertRow(-1);
    rowcut = tbody.rows.length - 1;
    for (var j = 0; j < 7; j++) {
        cell = document.createElement("TD");
        cell.className = "DetlCol";
        switch (j) {
            case 0:
                //刪除Button
                sHTML = "<input type='button' id='btnDelete" + rowcut + "' " + sType + " onclick='DeleteRow(event,this.id);' value='刪除'>"
                break;
            case 1:

                sHTML = " <span id='lblSEQ' > " + rowcut + " </span>"
                sHTML = sHTML + "<input type='hidden'  id='hidSEQ_D' value='" + rowcut + "'  />"
                break;
            case 2:
                sHTML = " <span id='lblcompany' >" + t_company + "</span>"
                sHTML = sHTML + "<input type='hidden'  id='hidCompany_D' value='" + company + "'  />"
                break;
            case 3:
                sHTML = " <span id='lblBU' >" + t_bu + "</span>"
                sHTML = sHTML + "<input type='hidden'  id='hidBU_D' value='" + bu + "'  />"
                break;
            case 4:
                sHTML = " <span id='lblZone' >" + t_zone + "</span>"
                sHTML = sHTML + "<input type='hidden'  id='hidzone_D' value='" + zone + "'  />"
                break;
            case 5:
                sHTML = " <span id='lblAREA' > " + t_AREA + " </span>"
                sHTML = sHTML + "<input type='hidden'  id='hidAREA_D' value='" + AREA + "'  />"
                break;
            case 6:
                sHTML = " <span id='lblDEPT' >" + t_Dept + "</span>"
                sHTML = sHTML + "<input type='hidden'  id='hidDept_D' value='" + Dept + "'  />"
                break;
        }
        cell.insertAdjacentHTML("afterBegin", sHTML);
        row.appendChild(cell);
    }
    }
    catch (err) {
        txt = "程式發生錯誤！\n";
        txt += "錯誤訊息: " + err.message + "\n\n";
        txt += "請按確定繼續.\n\n";
        alert(txt);
        save_error(prog_name, "Add_Condition", "USER", "js", err);
        return false;
    }
}

function getDept_Down(p_TargetDiv, p_val, p_opt) {
    try
    {
    var s_PARENT_DEPT_ID = "";
    var s_flag = "";
    var sDEPT_NO = "";
    var sNAME = ""
    sNAME = '#' + p_TargetDiv
    sDEPT_NO = $(sNAME).val();
    
    switch (p_TargetDiv) {
        case "ddlcompany":
            if (p_val == "0000")
            {
                s_OBJ_NAME = '#ddlcompany'
                sDEPT_NO = "0000";
                $('#ddlcompany').empty();
                $('#ddlcompany').append($("<option></option>").val("ALL").text("-All-"));
            }
            else
            {
                s_OBJ_NAME = '#ddlbu'
            }
            if (p_opt != "Y") {
                $("#MainContent_hidcompany").val(sDEPT_NO);
            }
            //清空DropDownList
            $('#ddlbu').empty();
            $('#ddlbu').append($("<option></option>").val("ALL").text("-All-"));
            $('#ddlzone').empty();
            $('#ddlzone').append($("<option></option>").val("ALL").text("-All-"));
            $('#ddlAREA').empty();
            $('#ddlAREA').append($("<option></option>").val("ALL").text("-All-"));
            $('#ddlDept').empty();
            $('#ddlDept').append($("<option></option>").val("ALL").text("-All-"));
            break;
        case "ddlbu":
            s_OBJ_NAME = '#ddlzone'
            if (p_opt != "Y") {
                $("#MainContent_hidbu").val(sDEPT_NO);
            }
            //清空DropDownList
            $('#ddlzone').empty();
            $('#ddlzone').append($("<option></option>").val("ALL").text("-All-"));
            $('#ddlAREA').empty();
            $('#ddlAREA').append($("<option></option>").val("ALL").text("-All-"));
            $('#ddlDept').empty();
            $('#ddlDept').append($("<option></option>").val("ALL").text("-All-"));
            break;
        case "ddlzone":
            s_OBJ_NAME = '#ddlAREA'
            if (p_opt != "Y") {
                $("#MainContent_hidzone").val(sDEPT_NO);
            }
            //清空DropDownList
            $('#ddlAREA').empty();
            $('#ddlAREA').append($("<option></option>").val("ALL").text("-All-"));
            $('#ddlDept').empty();
            $('#ddlDept').append($("<option></option>").val("ALL").text("-All-"));
            break;
        case "ddlAREA":
            s_OBJ_NAME = '#ddlDept'
            if (p_opt != "Y") {
                $("#MainContent_hidAREA").val(sDEPT_NO);
            }
            //清空DropDownList
            $('#ddlDept').empty();
            $('#ddlDept').append($("<option></option>").val("ALL").text("-All-"));
            break;
        case "ddlDept":
            if (p_opt != "Y") {
                $("#MainContent_hidDept").val(sDEPT_NO);
            }
            break;
        default:
            alert("載入錯誤!");
            break;
    }
    $.each(sDept, function (i, item) {
        //
        
        if (s_PARENT_DEPT_ID == "" || s_PARENT_DEPT_ID == item.PARENT_DEPT_ID || s_flag == "") {
            if (item.PARENT_DEPT_ID == sDEPT_NO) {
                $(s_OBJ_NAME).append($("<option></option>").val(item.BI4101).text(item.DEPT_NAME));
                s_flag = "Y";
            }
        }
        else {
            return false;
        }
        //上一筆
        s_PARENT_DEPT_ID = item.PARENT_DEPT_ID;

    });
    }
    catch (err) {
        txt = "程式發生錯誤！\n";
        txt += "錯誤訊息: " + err.message + "\n\n";
        txt += "請按確定繼續.\n\n";
        alert(txt);
        save_error(prog_name, "getDept_Down", "USER", "js", err);
        return false;
    }
}

function saveCondition() {
    try
    {
        var BULLETIN_ID = $("#MainContent_txt_BULLETIN_ID").val();
        //SEQ
        var sseq = "", s_seq = "";
        $("input[id*='hidSEQ_D']").each(function () {
            sseq = sseq + "'" + this.value + "',";
            s_seq = s_seq + this.value + ",";
        });
        sseq = "[" + sseq + "'']"
        $("#MainContent_hidseq_C").val(s_seq);
        //--------------------------------------------------------
        //Company
        var scompany = "", s_company = "", t_company = "";
        $("input[id*='hidCompany_D']").each(function () {
            scompany = scompany + "'" + this.value + "',";
            s_company = s_company + this.value + ",";

        });
        scompany = "[" + scompany + "'']"
        $("#MainContent_hidcompany_C").val(s_company);

        $("span[id*='lblcompany']").each(function () {
            t_company = t_company + this.innerHTML + ",";
        });
        $("#MainContent_hidcompany_C_T").val(t_company);
        //--------------------------------------------------------


        //BU
        var sbu = "", s_bu = "", t_bu = "";
        $("input[id*='hidBU_D']").each(function () {
            sbu = sbu + "'" + this.value + "',";
            s_bu = s_bu + this.value + ",";
        });
        sbu = "[" + sbu + "'']"
        $("#MainContent_hidbu_C").val(s_bu);

        $("span[id*='lblBU']").each(function () {
            t_bu = t_bu + this.innerHTML + ",";
        });
        $("#MainContent_hidbu_C_T").val(t_bu);
        //--------------------------------------------------------

        //ZONE
        var szone = "", s_zone = "", t_zone = "";
        $("input[id*='hidzone_D']").each(function () {
            szone = szone + "'" + this.value + "',";
            s_zone = s_zone + this.value + ",";

        });
        szone = "[" + szone + "'']"
        $("#MainContent_hidzone_C").val(s_zone);

        $("span[id*='lblZone']").each(function () {
            t_zone = t_zone + this.innerHTML + ",";
        });
        $("#MainContent_hidzone_C_T").val(t_zone);
        //--------------------------------------------------------
        //AREA
        var sAREA = "", s_AREA = "", t_area = "";
        $("input[id*='hidAREA_D']").each(function () {
            sAREA = sAREA + "'" + this.value + "',";
            s_AREA = s_AREA + this.value + ",";

        });
        sAREA = "[" + sAREA + "'']"
        $("#MainContent_hidarea_C").val(s_AREA);

        $("span[id*='lblAREA']").each(function () {
            t_area = t_area + this.innerHTML + ",";
        });
        $("#MainContent_hidarea_C_T").val(t_area);
        //--------------------------------------------------------

        //Dept
        var sDept = "", s_Dept = "", t_dept = "";
        $("input[id*='hidDept_D']").each(function () {
            sDept = sDept + "'" + this.value + "',"
            s_Dept = s_Dept + this.value + ",";
        });
        sDept = "[" + sDept + "'']"
        $("#MainContent_hiddept_C").val(s_Dept);

        $("span[id*='lblDEPT']").each(function () {
            t_dept = t_dept + this.innerHTML + ",";
        });
        $("#MainContent_hiddept_C_T").val(t_dept);
        //--------------------------------------------------------


    }
    catch (err) {
        txt = "程式發生錯誤！\n";
        txt += "錯誤訊息: " + err.message + "\n\n";
        txt += "請按確定繼續.\n\n";
        alert(txt);
        save_error(prog_name, "saveCondition", "USER", "js", err);
        return false;
    }
    

}

function check() {
    try
    {
    if ($("#MainContent_hidseq_C").val() == "") {
        alert("請新增公告對像！")
        return false;
    }
    
    //日期檢核
    if (isdate($("#MainContent_txt_START_DATE").val()) != true) {
        alert("公告起始日期格式有誤")
        $("#MainContent_txt_START_DATE").focus();
        return false;
    }
    if ($("#MainContent_txt_END_DATE_txtDate").val() == "") {
        $("#MainContent_txt_END_DATE_txtDate").val("20201231");
    }
    if (isdate($("#MainContent_txt_END_DATE_txtDate").val()) != true) {
        alert("公告截止日期格式有誤")
        $("#MainContent_txt_END_DATE_txtDate").focus();
        return false;
    }
    if ($("#MainContent_txt_END_DATE_txtDate").val() < $("#MainContent_txt_START_DATE").val()) {
        alert("公告起始日不可小於公告截止日")
        $("#MainContent_txt_START_DATE").focus();
        return false;
    }
    if (smode == "add") {
        if ($("#MainContent_txt_START_DATE").val() < $("#MainContent_hidSYSDATE").val()) {
            alert("公告起始日不得小於今日")
            $("#MainContent_txt_START_DATE").focus();
            return false;
        }
    }


    if ($("#MainContent_hid_BULLETIN_TEXT_TW").val() == "" && $("#MainContent_hid_BULLETIN_TEXT_GB").val() == "" && $("#MainContent_hid_BULLETIN_TEXT_EN").val() == "") {
        alert("請輸入大綱！")
        return false;
    }
    if ( $("#MainContent_hid_BULLETIN_SPEC_TW").val() == "" && $("#MainContent_hid_BULLETIN_SPEC_GB").val() == "" && $("#MainContent_hid_BULLETIN_SPEC_EN").val() == "") {
        alert("請輸入公告內容！")
        return false;
    }
    if ($("#MainContent_hid_BULLETIN_TEXT_TW").val() != "" && $("#MainContent_hid_BULLETIN_SPEC_TW").val() == "") {
        alert("請輸入繁體中文公告內容！")
        return false;
    }
    if ($("#MainContent_hid_BULLETIN_TEXT_TW").val() == "" && $("#MainContent_hid_BULLETIN_SPEC_TW").val() != "") {
        alert("請輸入繁體中文大綱！")
        return false;
    }
    if ($("#MainContent_hid_BULLETIN_TEXT_GB").val() != "" && $("#MainContent_hid_BULLETIN_SPEC_GB").val() == "") {
        alert("請輸入簡體中文公告內容！")
        return false;
    }
    if ($("#MainContent_hid_BULLETIN_TEXT_GB").val() == "" && $("#MainContent_hid_BULLETIN_SPEC_GB").val() != "") {
        alert("請輸入簡體中文大綱！")
        return false;
    }
    if ($("#MainContent_hid_BULLETIN_TEXT_EN").val() != "" && $("#MainContent_hid_BULLETIN_SPEC_EN").val() == "") {
        alert("請輸入英文公告內容！")
        return false;
    }
    if ($("#MainContent_hid_BULLETIN_TEXT_EN").val() == "" && $("#MainContent_hid_BULLETIN_SPEC_EN").val() != "") {
        alert("請輸入英文大綱！")
        return false;
    }
    //alert($("#MainContent_hid_BULLETIN_SPEC_EN").val());
    TRANS();
    }
    catch (err) {
        txt = "程式發生錯誤！\n";
        txt += "錯誤訊息: " + err.message + "\n\n";
        txt += "請按確定繼續.\n\n";
        alert(txt);
        save_error(prog_name, "check", "USER", "js", err);
        return false;
    }
   
}

function TRANS() {
    try
    {
        //TW
        $("#MainContent_hid_BULLETIN_TEXT_TW").val(htmlDecode($("#MainContent_hid_BULLETIN_TEXT_TW").val()));
        $("#MainContent_hid_BULLETIN_TEXT_TW").val(htmlEncode($("#MainContent_hid_BULLETIN_TEXT_TW").val()));
        $("#MainContent_hid_BULLETIN_SPEC_TW").val(htmlDecode($("#MainContent_hid_BULLETIN_SPEC_TW").val()));
        $("#MainContent_hid_BULLETIN_SPEC_TW").val(htmlEncode($("#MainContent_hid_BULLETIN_SPEC_TW").val()));
        //GB
        $("#MainContent_hid_BULLETIN_TEXT_GB").val(htmlDecode($("#MainContent_hid_BULLETIN_TEXT_GB").val()));
        $("#MainContent_hid_BULLETIN_TEXT_GB").val(htmlEncode($("#MainContent_hid_BULLETIN_TEXT_GB").val()));
        $("#MainContent_hid_BULLETIN_SPEC_GB").val(htmlDecode($("#MainContent_hid_BULLETIN_SPEC_GB").val()));
        $("#MainContent_hid_BULLETIN_SPEC_GB").val(htmlEncode($("#MainContent_hid_BULLETIN_SPEC_GB").val()));
        //EN
        $("#MainContent_hid_BULLETIN_TEXT_EN").val(htmlDecode($("#MainContent_hid_BULLETIN_TEXT_EN").val()));
        $("#MainContent_hid_BULLETIN_TEXT_EN").val(htmlEncode($("#MainContent_hid_BULLETIN_TEXT_EN").val()));
        $("#MainContent_hid_BULLETIN_SPEC_EN").val(htmlDecode($("#MainContent_hid_BULLETIN_SPEC_EN").val()));
        $("#MainContent_hid_BULLETIN_SPEC_EN").val(htmlEncode($("#MainContent_hid_BULLETIN_SPEC_EN").val()));
    }
    catch (err) {
        txt = "程式發生錯誤！\n";
        txt += "錯誤訊息: " + err.message + "\n\n";
        txt += "請按確定繼續.\n\n";
        alert(txt);
        save_error(prog_name, "TRANS", "USER", "js", err);
        return false;
    }
}

function htmlEncode(value) {
    //create a in-memory div, set it's inner text(which jQuery automatically encodes)
    //then grab the encoded contents back out.  The div never exists on the page.
    return $('<div/>').text(value).html();
}

function htmlDecode(value) {
    return $('<div/>').html(value).text();
}
function check_date(source, arguments) {
    if (isdate(arguments.Value)) {
        arguments.IsValid = true;
    }
    else {
        alert("日期格式有誤！");
        arguments.IsValid = false;
    }
}

function isdate(sTmpVal) {
    //01-字串長度不可以小於8
    if (sTmpVal.length < 8) {
        return false;
    }

    //02-字串的值不為 0~9 或 "/" SJ Add 2009/10/07
    for (var i = 0; i < 8; i++) {
        var letter = sTmpVal.substring(i, i + 1);
        if (letter < "0" || "9" < letter)
        {
            return false;
        }
    }

    //03-字串第六碼第七碼的值是否為月份
    var iYear = parseInt(sTmpVal.substring(0, 4));

    var sDummy = sTmpVal.substr(4, 2);
    //if (sDummy == "0") {
    //    sDummy = sTmpVal.substring(5, 6);
    //}
    //else {
    //    sDummy = sTmpVal.substring(4, 6);
    //}
    var iMonth = parseInt(sDummy);
    if (iMonth < 1 || 12 < iMonth) {
        return false;
    }

    //==== SJ Mark Begin 2009/10/07  
    //  for (var i=8;i<10;i++) {
    //    var letterDD=sTmpVal.substring(i,i+1);
    //    if (letterDD<"0" || "9"<letterDD) {
    //      return false;
    //    }
    //  }
    //==== SJ Mark End

    //04-字串第九碼第十碼的值是否為天數
    var sDummy1 = sTmpVal.substr(6, 2);
    //if (sDummy1 == "0") {
    //    sDummy1 = sTmpVal.substring(9, 10);
    //}
    //else {
    //    sDummy1 = sTmpVal.substring(8, 10);
    //}
    var iDay = parseInt(sDummy1);
    if (iDay < 1 || 31 < iDay) {
        return false;
    } else if ((iMonth == 4 || iMonth == 6 || iMonth == 9 || iMonth == 11) && (iDay < 1 || 30 < iDay)) {
        return false;
    } else if (iMonth == 2 && (iDay < 1 || 29 < iDay)) {
        return false;
    } else if (iMonth == 2 && LeapYear(iYear) == false && (iDay < 1 || 28 < iDay)) {
        return false;
    }
    return true;
}
function LeapYear(intYear) {
    if (intYear % 100 == 0) {
        if (intYear % 400 == 0) { return true; }
    }
    else {
        if ((intYear % 4) == 0) { return true; }
    }
    return false;
}

